<%@page import="com.nxu.entity.BookBean"%>
<%@page import="com.nxu.entity.CartItemBean"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.text.DecimalFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>书香阁 - 购物车</title>
    <style>
        :root {
            --primary-color: #8d6e63;
            --light-gray: #f5f5f5;
            --dark-gray: #5a3921;
            --text-color: #333;
            --border-color: #d7ccc8;
            --success-color: #388e3c;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), 
                        url('https://images.unsplash.com/photo-1507842217343-583bb7270b66?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80') no-repeat center center fixed;
            background-size: cover;
            color: var(--text-color);
        }
        
        .container {
            max-width: 1200px;
            margin: 30px auto;
            background: rgba(255, 255, 255, 0.95);
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.2);
            backdrop-filter: blur(5px);
            animation: fadeIn 0.5s ease-in-out;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        h1 {
            text-align: center;
            color: var(--dark-gray);
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid var(--primary-color);
        }
        
        .empty-cart {
            text-align: center;
            padding: 40px;
            color: var(--dark-gray);
            font-size: 18px;
        }
        
        .empty-cart i {
            font-size: 50px;
            color: #bdbdbd;
            margin-bottom: 20px;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            border-radius: 8px;
            overflow: hidden;
        }
        
        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }
        
        th {
            background-color: var(--light-gray);
            font-weight: 600;
            color: var(--dark-gray);
        }
        
        tr:hover {
            background-color: rgba(141, 110, 99, 0.05);
        }
        
        .book-title {
            font-weight: 500;
            color: var(--dark-gray);
        }
        
        .book-cover {
            width: 50px;
            height: 70px;
            object-fit: cover;
            border-radius: 3px;
            margin-right: 15px;
            vertical-align: middle;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .quantity {
            font-weight: 500;
            color: var(--dark-gray);
        }
        
        .price {
            font-weight: 500;
            color: var(--dark-gray);
        }
        
        .subtotal {
            font-weight: bold;
            color: var(--dark-gray);
        }
        
        .total-row {
            font-size: 18px;
            background-color: var(--light-gray);
        }
        
        .total-label {
            font-weight: bold;
            color: var(--dark-gray);
        }
        
        .total-amount {
            font-weight: bold;
            color: var(--primary-color);
            font-size: 20px;
        }
        
        .buttons {
            margin-top: 40px;
            display: flex;
            justify-content: space-between;
        }
        
        .btn {
            padding: 12px 25px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }
        
        .btn-continue {
            background-color: #f5f5f5;
            color: var(--dark-gray);
            border: 1px solid var(--border-color);
        }
        
        .btn-continue:hover {
            background-color: #e0e0e0;
            transform: translateY(-2px);
        }
        
        .btn-checkout {
            background-color: var(--success-color);
            color: white;
            border: none;
        }
        
        .btn-checkout:hover {
            background-color: #2e7d32;
            transform: translateY(-2px);
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 20px;
            }
            
            th, td {
                padding: 10px;
            }
            
            .buttons {
                flex-direction: column;
                gap: 15px;
            }
            
            .btn {
                width: 100%;
            }
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body>
    <div class="container">
        <h1><i class="fas fa-shopping-cart"></i> 我的购物车</h1>
        
        <%
            Map<String, CartItemBean> cart = (Map<String, CartItemBean>) session.getAttribute("cart");
            double total = 0;
            if (cart == null || cart.isEmpty()) {
        %>
        <div class="empty-cart">
            <i class="fas fa-book-open"></i>
            <p>您的购物车还是空的，快去挑选心仪的书籍吧！</p>
            <a href="list.jsp" class="btn btn-continue">
                <i class="fas fa-book"></i> 去逛逛
            </a>
        </div>
        <%
            } else {
                Set<String> cartItems = cart.keySet();
                Iterator<String> iterator = cartItems.iterator();
                BookBean book;
                CartItemBean cartItem;
                int quantity;
                double price, subtotal;
        %>
        <table>
            <thead>
                <tr>
                    <th>书籍信息</th>
                    <th>数量</th>
                    <th>单价</th>
                    <th>小计</th>
                </tr>
            </thead>
            <tbody>
                <%
                    while (iterator.hasNext()) {
                        String isbn = iterator.next();
                        cartItem = cart.get(isbn);
                        book = cartItem.getBook();
                        quantity = cartItem.getQuantity();
                        price = book.getPrice();
                        subtotal = price * quantity;
                        total += subtotal;
                %>
                <tr>
                    <td class="book-title">
                        <img src="images/<%=book.getImagefile()%>" class="book-cover" 
                             onerror="this.src='images/default-book.jpg'" alt="<%=book.getTitle()%>">
                        <%=book.getTitle()%>
                    </td>
                    <td class="quantity">
                        <%=quantity%>
                    </td>
                    <td class="price">￥<%=new DecimalFormat("0.00").format(price)%></td>
                    <td class="subtotal">￥<%=new DecimalFormat("0.00").format(subtotal)%></td>
                </tr>
                <% } %>
                <tr class="total-row">
                    <td colspan="3" class="total-label">总计：</td>
                    <td class="total-amount">￥<%=new DecimalFormat("0.00").format(total)%></td>
                </tr>
            </tbody>
        </table>
        
        <div class="buttons">
            <a href="list.jsp" class="btn btn-continue">
                <i class="fas fa-arrow-left"></i> 继续购物
            </a>
            <form action="order_confirm.jsp" method="post">
                <button type="submit" class="btn btn-checkout">
                    <i class="fas fa-credit-card"></i> 去结算
                </button>
            </form>
        </div>
        <%
                session.setAttribute("total", new Double(total));
            }
        %>
    </div>
</body>
</html>